﻿using Newtonsoft.Json;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CHS.ChongQing.Service
{
    public class CHSBusinessService
    {

        [DllImport("SiInterface_hsf.dll", CharSet = CharSet.Ansi, EntryPoint = "INIT", CallingConvention = CallingConvention.StdCall)]
        private static extern int INIT( IntPtr errMsg);
        
        [DllImport("SiInterface_hsf.dll",/* CharSet = CharSet.Ansi,*/ EntryPoint = "BUSINESS_HANDLE"/*, CallingConvention = CallingConvention.StdCall*/)]
        private static extern int BUSINESS_HANDLE(IntPtr inputData, IntPtr outputData);

        public string callCqService()
        {
            IntPtr errMsgPtr = Marshal.AllocHGlobal(200 * sizeof(char));
            IntPtr inputPtr;
            IntPtr outputPtr = Marshal.AllocHGlobal(50000 * sizeof(char));
            StringBuilder strInput = new StringBuilder();
            StringBuilder strOutput = new StringBuilder(capacity: 50000);
            string inputStr = "{\"infno\":\"9001\",\"msgid\":\"H50010504793202411182124060001\",\"mdtrtarea_admvs\":\"500105\",\"insuplc_admdvs\":\"500105\",\"recer_sys_code\":\"FSI_LOCAL\",\"dev_no\":\"\",\"dev_safe_info\":\"\",\"cainfo\":\"\",\"signtype\":\"\",\"infver\":\"V1.0\",\"opter_type\":\"1\",\"opter\":\"0\",\"inf_time\":\"2024-11-18 21:24:06\",\"fixmedins_code\":\"H50010504793\",\"fixmedins_name\":\"重庆景城胃肠医院\",\"sign_no\":\"\",\"input\":{\"signIn\":{\"Id\":0,\"opter_no\":\"9999\",\"mac\":\"902E16B2C3D8\",\"ip\":\"0.0.0.1\",\"sign_time\":\"0001-01-01 00:00:00\",\"IsDelete\":false}}}";
            string outputStr = "";

            strInput.Append(strInput);
            inputPtr = Marshal.StringToCoTaskMemAnsi(inputStr);

            var status = INIT(errMsgPtr);

            string errMsg = Marshal.PtrToStringAnsi(errMsgPtr);

            status = BUSINESS_HANDLE(inputPtr,  outputPtr);
            outputStr = Marshal.PtrToStringAnsi(outputPtr);
            Marshal.FreeHGlobal(outputPtr);
            return (errMsgPtr == IntPtr.Zero) ? null : "";
        }


        public CHSOutput CallService(CHSBaseInput input)
        {
            if (input.Input==null)
            {
                return new CHSOutput
                {
                    Code = -1,
                    Message = "交易输入不能为空"
                };
            }

            string inputString = input.Input.ToString();

            IntPtr errMsgPtr = Marshal.AllocHGlobal(2000 * sizeof(char));
            IntPtr inputData;
            IntPtr outputData = Marshal.AllocHGlobal(50000 * sizeof(char));

            if (outputData == IntPtr.Zero)
            {
                return new CHSOutput
                {
                    Code = -1,
                    Message = "内存分配失败"
                };
            }

            var status = INIT( errMsgPtr);
            string initMsg = Marshal.PtrToStringAnsi(errMsgPtr);
            if (status != 0)
            {
                return new CHSOutput
                {
                    Code = status,
                    Message = $"动态库初始化失败，失败信息{Marshal.PtrToStringAnsi(errMsgPtr)}"
                };
            }

            inputData = Marshal.StringToHGlobalAnsi(inputString);
            //Thread.Sleep(1000);
            status = BUSINESS_HANDLE(inputData, outputData);
            var result = Marshal.PtrToStringAnsi(outputData);

            Marshal.FreeHGlobal(errMsgPtr);
            Marshal.FreeHGlobal(outputData);

            return new CHSOutput
            {
                Code = status,
                Message = $"接口调用成功",
                Result = result
            };

        }
    }
}
